package cn.wolfcode.Controller;


import cn.wolfcode.domain.Business;
import cn.wolfcode.qo.BussinessObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.FileUploadUtil;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;

@Controller
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private IBusinessService businessService;


    @RequirePermission(name = "门店页面",experssion = "business:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BussinessObject qo){
        PageInfo<Business> pageInfo = businessService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "business/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "门店删除",experssion = "business:delete")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            businessService.delete(id);
        }
        return new jsonResult();
    }


    @Autowired
    private ServletContext servletContext;



    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "门店新增/修改",experssion = "business:saveOrUpdate")
    public String saveOrUpdate(Business business, MultipartFile file) throws Exception {

        //因为可能前端没有上传文件，而直接本身就有文件，
        // 所以我们需要写个动态sql来进行插入来进行动态更改
        //如果没传的话，则不修改路径
        //判断用户是否上传文件
        if (file != null && file.getSize() > 0){
            //获取webapp的绝对路径
            String realPath = servletContext.getRealPath("/");
            System.out.println(realPath);

            //先把之前的图片给删除掉
            if (StringUtils.hasText(business.getLicense_img())) {
                FileUploadUtil.deleteFile(realPath+business.getLicense_img());
                System.out.println(realPath + business.getLicense_img());
            }
            //获取图片所在的路径,使用工具方法向目录存入文件
            String filePath = FileUploadUtil.uploadFile(file, realPath);
//            把图片地址存入到对象里
            business.setLicense_img(filePath);
        }


        if (business.getId() != null) {
            businessService.update(business);
        }else {
            businessService.save(business);
        }
        return "redirect:/business/list";
    }


    @RequestMapping("/input")
    @RequirePermission(name = "门店新增/修改",experssion = "business:saveOrUpdate")
    public String input(Long id,Model model){
        if (id != null){
            Business business = businessService.get(id);
            model.addAttribute("business",business);
        }
        return "business/input";
    }
}
